﻿using System;
using System.Linq;
using System.Net;
using System.Web.Mvc;
using Matrix.MaKaYunShang.BLL;
using Matrix.MaKaYunShang.Model;

namespace Matrix.MaKaYunShang.MagWeb.Controllers
{
    [Authorization]
    [Matrix.MaKaYunShang.Model.Attributes.Skip]
    public class CouponCategoryController : BaseController
    {
        public ActionResult List()
        {
            return View();
        }

        [HttpPost]
        [Matrix.MaKaYunShang.Model.Attributes.Skip]
        public JsonResult Listing(int draw, int start, int length)
        {
            try
            {
                int order;
                int.TryParse(Request["order[0][column]"], out order);
                var direction = Request["order[0][dir]"];
                var orderByColumns = new[] { "CategoryName", "CategoryCode", "Description" };
                var search = Request["search[value]"];
                var list = CouponCategoryBLL.Instance.GetPagingList(draw, (start / length) + 1, length, SqlSafe(search), orderByColumns[order] + " " + direction);
                return Json(list);
            }
            catch (Exception ex)
            {
                Logger.Error("Coupon Listing Catch Error: " + ex.Message);
                return Json(false);
            }
        }

        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public JsonResult Create(CouponCategory model)
        {
            var result = new Result();
            var checkModel = CouponCategoryBLL.Instance.GetModelList("CategoryCode='" + SqlSafe(model.CategoryCode) + "'").FirstOrDefault();
            if (checkModel != null)
            {
                result.Message = "类别编码已存在，请更换";
                return Json(result);
            }
            result.Status = CouponCategoryBLL.Instance.Add(model) > 0;
            if (result.Status == false)
            {
                result.Message = "添加失败，请重试";
            }
            return Json(result);
        }

        public ActionResult Update(int id)
        {
            if (id <= 0)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            var model = CouponCategoryBLL.Instance.GetModel(id);
            if (model == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.NotFound);
            }
            return View(model);
        }

        [HttpPost]
        public JsonResult Update(CouponCategory model)
        {
            var result = new Result();
            var temp = CouponCategoryBLL.Instance.GetModel(model.Id);
            if (temp == null)
            {
                result.Message = "保存失败，请重试";
                return Json(result);
            }
            temp.CategoryName = model.CategoryName;
            temp.Description = model.Description;
            result.Status = CouponCategoryBLL.Instance.Update(temp);
            if (result.Status == false)
            {
                result.Message = "保存失败，请重试";
            }
            return Json(result);
        }

        [HttpPost]
        public JsonResult Delete()
        {
            var result = CouponCategoryBLL.Instance.DeleteList(Request["doIds"]);
            return Json(result);
        }
    }
}
